package cn.mafangui.hotel.controller.worker;

import cn.mafangui.hotel.entity.CheckIn;
import cn.mafangui.hotel.entity.Order;
import cn.mafangui.hotel.entity.Room;
import cn.mafangui.hotel.mapper.CheckInMapper;
import cn.mafangui.hotel.mapper.OrderMapper;
import cn.mafangui.hotel.mapper.RoomMapper;
import cn.mafangui.hotel.response.AjaxResult;
import cn.mafangui.hotel.response.ResponseTool;
import cn.mafangui.hotel.service.CheckInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/op/check-in")
@CrossOrigin("*")
public class OpCheckInController {

    @Autowired
    private CheckInService checkInService;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private CheckInMapper checkInMapper;

    @RequestMapping(value = "/in")
    public AjaxResult addCheckIn(int orderId, int peoCount, String persons){
        CheckIn checkIn = new CheckIn();
        checkIn.setOrderId(orderId);
        checkIn.setPeoCount(peoCount);
        checkIn.setPersons(persons);
        //checkIn.setIds(ids);

        // 订单信息
        Order order = orderMapper.selectByPrimaryKey(orderId);
        Room room = roomMapper.selectByNumber(order.getRoomNumber());
        room.setRoomStatus(-1);
        roomMapper.updateByPrimaryKey(room);

        return ResponseTool.success(1);
    }

    @RequestMapping(value = "/out")
    @Transactional
    public AjaxResult checkOut(String roomNumber) {
//        if(checkInService.checkOut(roomNumber)<0)
//            return ResponseTool.failed("退房失败");
        Room room = roomMapper.selectByNumber(roomNumber);
        room.setRoomStatus(1);
        roomMapper.updateByPrimaryKey(room);

        for (Order order : orderMapper.selectAll()) {
            if (order.getRoomNumber().equals(roomNumber)){
                order.setOrderStatus(2);
                orderMapper.updateByPrimaryKeySelective(order);
                break;
            }
        }

        return ResponseTool.success("退房成功");
    }


    @RequestMapping(value = "/delete")
    public AjaxResult deleteCheckIn(int checkId){
        if(checkInService.delete(checkId)!=1)
            return ResponseTool.failed("删除失败");
        return ResponseTool.success("删除成功");
    }

    @RequestMapping(value = "/update")
    public AjaxResult update(int checkId,String roomNumber){
        CheckIn checkIn = new CheckIn();
        checkIn.setCheckInId(checkId);
        checkIn.setRoomNumber(roomNumber);
        if(checkInService.update(checkIn)!=1)
            return ResponseTool.failed("更新失败");
        return ResponseTool.success("更新成功");
    }



    @RequestMapping(value = "/{checkId}")
    public AjaxResult getById(@PathVariable int checkId){
        return ResponseTool.success(checkInService.selectById(checkId));
    }

    @RequestMapping(value = "")
    public AjaxResult getAll(){
        return ResponseTool.success(checkInService.selectAll());
    }

}
